package com.buba.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.pojo.Vendor;
import com.buba.service.VendorService;
import com.guli.common.ov.R;
import com.guli.common.utils.AliyunOSSUtil;
import com.guli.common.utils.IdWorker;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 厂商表 前端控制器
 * </p>
 *
 * @author MrJia
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/vendor")
public class VendorController {

    @Autowired
    private VendorService vendorService;

    @RequestMapping("add")
    public R add(Vendor vendor, MultipartFile businessLicenseFile, MultipartFile signContentFile) throws IOException {
        if (vendor.getId() != null) {
            //oss 上传图片
//            String businessLicenseFileUrl = "/home/c/图片/" + UUID.randomUUID() + new Date(System.currentTimeMillis()) + ".jpg";
//            String signContentFileUrl = "/home/c/图片/" + UUID.randomUUID() + new Date(System.currentTimeMillis()) + ".jpg";
//            businessLicenseFile.transferTo(new File(businessLicenseFileUrl));
//            signContentFile.transferTo(new File(signContentFileUrl));
            String fileName1 = businessLicenseFile.getOriginalFilename();
            String fileName2 = signContentFile.getOriginalFilename();
            File file1 = new File(fileName1);
            File file2 = new File(fileName2);
            FileUtils.copyInputStreamToFile(businessLicenseFile.getInputStream(), file1);
            FileUtils.copyInputStreamToFile(signContentFile.getInputStream(), file2);
            String upload = AliyunOSSUtil.upload(file1);
            String upload1 = AliyunOSSUtil.upload(file2);
            vendor.setBusinessLicense(upload);
            vendor.setSignContent(upload1);
            vendor.setState("24");
            vendor.setReviewStatus("0");
        }
        //添加厂商
        boolean saveOrUpdate = vendorService.saveOrUpdate(vendor);
        if (saveOrUpdate) {
            return R.ok().message("操作成功");
        }
        return R.error().message("操作失败");
    }

    @ApiOperation(value = "分页查询厂商表", httpMethod = "POST")
    @RequestMapping("/selectList")
    public R selectList(Integer page, Integer limit, Vendor vendor) {
//        return R.ok().data("vendors", vendorService.selectList(page, limit, vendor));
        Page<Vendor> page1 = new Page<>(page, limit);
        return R.ok().data("vendors", vendorService.selectListByCondition(page1, vendor));
    }

    @ApiOperation(value = "新增签约", httpMethod = "POST")
    @RequestMapping("updateDept")
    public R updateDept(Vendor vendor) {
        boolean b = vendorService.updateDept(vendor);
        if (b) return R.ok().message("新增成功");
        return R.error().message("系统繁忙");
    }
}

